package com.phiture.erp.purchase.web.controller.admin.api;

import cn.hutool.core.collection.CollUtil;
import com.phiture.erp.purchase.api.pojo.req.ErpPurchaseInReq;
import com.phiture.erp.purchase.api.pojo.resp.ErpPurchaseInItemResp;
import com.phiture.erp.purchase.api.pojo.resp.ErpPurchaseInResp;
import com.phiture.erp.purchase.api.service.ErpPurchaseInApi;
import com.phiture.erp.purchase.core.service.ErpPurchaseInService;
import com.phiture.erp.purchase.dal.entity.ErpPurchaseInDO;
import com.phiture.erp.purchase.dal.entity.ErpPurchaseInItemDO;
import javafx.util.Pair;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author YH
 * @date 2025-06-12-14:13
 */

@Service
@RequiredArgsConstructor
public class ErpPurchaseInApiServiceImpl implements ErpPurchaseInApi {
    private final ErpPurchaseInService purchaseInService;

    @Override
    public List<ErpPurchaseInResp> getPurchaseInList(ErpPurchaseInReq req) {
        List<ErpPurchaseInDO> purchaseInList = purchaseInService.getPurchaseInList(req.getStatus(), req.getStartDate(), req.getEndDate());
        return CollUtil.isEmpty(purchaseInList) ? Collections.emptyList() : purchaseInList.stream().map(item -> {
            ErpPurchaseInResp resp = new ErpPurchaseInResp();
            resp.setPurchaseInId(item.getId());
            resp.setPurchaseInNo(item.getNo());
            resp.setPurchaseInStatus(item.getStatus());
            resp.setInTime(item.getInTime());
            resp.setSupplierId(item.getSupplierId());
            resp.setTotalCount(item.getTotalCount());
            resp.setTotalPrice(item.getTotalPrice());
            return resp;
        }).collect(Collectors.toList());
    }

    @Override
    public List<ErpPurchaseInItemResp> getPurchaseInItemList(Map<Long, javafx.util.Pair<String, Long>> purchaseInMap) {
        List<ErpPurchaseInItemDO> purchaseInItemList = purchaseInService.getPurchaseInItemListByInIds(purchaseInMap.keySet());
        return CollUtil.isEmpty(purchaseInItemList) ? Collections.emptyList() : purchaseInItemList .stream().map(item ->{
            ErpPurchaseInItemResp itemResp = new ErpPurchaseInItemResp();
            itemResp.setPurchaseInId(item.getInId());
            Pair<String, Long> pair = purchaseInMap.get(item.getInId());
            if(pair != null){
                itemResp.setPurchaseInNo(pair.getKey());
                itemResp.setSupplierId(pair.getValue());
            }
            itemResp.setPurchaseInItemId(item.getId());
            itemResp.setOrderItemId(item.getOrderItemId());
            itemResp.setWarehouseId(item.getWarehouseId());
            itemResp.setMaterialId(item.getMaterialId());
            itemResp.setMaterialUnitId(item.getMaterialUnitId());
            itemResp.setMaterialPrice(item.getMaterialPrice());
            itemResp.setCount(item.getCount());
            itemResp.setTotalPrice(item.getTotalPrice());
            itemResp.setTaxPercent(item.getTaxPercent());
            itemResp.setTaxPrice(item.getTaxPrice());
            return itemResp;
        }).collect(Collectors.toList());
    }

    @Override
    public void backfillPurchaseInAccountCheckFlag(Collection<Long> purchaseInIds, Boolean accountCheckFlag) {
        purchaseInService.backfillPurchaseInAccountCheckFlag(purchaseInIds, accountCheckFlag);
    }

    @Override
    public void updatePurchaseInAccountCheckFlag(Collection<String> purchaseInNos, Boolean accountCheckFlag) {
        purchaseInService.updatePurchaseInAccountCheckFlag(purchaseInNos, accountCheckFlag);
    }
}
